package mall

import (
	"fmt"
	"time"

	"gorm.io/gorm"
	"main.go/global"
	"main.go/model/mall"
	mallReq "main.go/model/mall/request"
	"main.go/utils"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: Register
//@description: 用户注册
//@param: u model.SysUser
//@return: userInter system.SysUser, err error

type GoodsInfoService struct{}

func (s *GoodsInfoService) Create(u *mall.MallGoodsInfo) (out *mall.MallGoodsInfo, err error) {
	err = global.EVA_DB.Create(&u).Error
	return u, err
}

func (s *GoodsInfoService) Delete(id int) (err error) {
	// 事务
	return global.EVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("id = ?", id).Delete(&mall.MallGoodsInfo{}).Error; err != nil {
			return err
		}
		return nil
	})
}

func (s *GoodsInfoService) Update(req *mall.MallGoodsInfo) (out *mall.MallGoodsInfo, err error) {
	updateData := make(map[string]interface{})
	if len(req.Name) > 0 {
		updateData["name"] = req.Name
	}
	if len(req.Intro) > 0 {
		updateData["intro"] = req.Intro
	}
	if req.CategoryId != 0 {
		updateData["category_id"] = req.CategoryId
	}

	if len(req.CoverImg) > 0 {
		updateData["cover_img"] = req.CoverImg
	}
	if len(req.Carousel) > 0 {
		updateData["carousel"] = req.Carousel
	}
	if len(req.Details) > 0 {
		updateData["details"] = req.Details
	}
	if req.OriginalPrice != 0 {
		updateData["original_price"] = req.OriginalPrice
	}
	if req.SellingPrice != 0 {
		updateData["selling_price"] = req.SellingPrice
	}
	if req.StockNum != nil {
		updateData["stock_num"] = req.StockNum
	}
	if req.Type != 0 {
		updateData["type"] = req.Type
	}

	if len(req.Tag) > 0 {
		updateData["tag"] = req.Tag
	}

	if req.UpdateUserId != 0 {
		updateData["update_user_id"] = req.UpdateUserId
	}

	if req.SellStatus != 0 {
		updateData["sell_status"] = req.SellStatus
	}

	err = global.EVA_DB.Model(&mall.MallGoodsInfo{}).Where(mall.MallGoodsInfo{
		EVA_MODEL: global.EVA_MODEL{
			ID: req.ID,
		},
	}).Updates(updateData).Error
	return out, err
}

func (s *GoodsInfoService) List(req *mallReq.MallOrderInfoListReq) (list []*mall.MallGoodsInfo, total int64, err error) {
	limit := req.PageSize
	offset := req.PageSize * (req.Page - 1)
	fmt.Println(limit, offset)
	db := global.EVA_DB.Model(&mall.MallGoodsInfo{})
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(req.Name) != 0 {
		db = db.Where("name LIKE ?", "%"+req.Name+"%")
	}
	if len(req.Tag) != 0 {
		db = db.Where("tag LIKE ?", "%"+req.Tag+"%")
	}
	if req.CategoryId != 0 {
		db = db.Where("category_id = ?", req.CategoryId)
	}
	if req.StockNum != nil {
		db = db.Where("stock_num = ?", req.StockNum)
	}
	if req.Type != 0 {
		db = db.Where("type = ?", req.Type)
	}
	if req.SellStatus != 0 {
		db = db.Where("sell_status = ?", req.SellStatus)
	}

	if req.ID != 0 {
		db = db.Where("id = ?", req.ID)
	}

	if req.StartCreatedAt != 0 && req.EndCreatedAt != 0 {
		t1 := time.Unix(req.StartCreatedAt, 0).In(utils.ShLocation)
		t2 := time.Unix(req.EndCreatedAt, 0).In(utils.ShLocation)
		db = db.Where("created_at BETWEEN ? AND ?", t1, t2)
	}
	err = db.Count(&total).Error
	if err != nil {
		return list, total, err
	}

	err = db.Order("id desc").Limit(limit).Offset(offset).Find(&list).Error
	return list, total, err
}

func (s *GoodsInfoService) Count(req *mall.MallGoodsInfo) (total int64, err error) {
	db := global.EVA_DB.Model(&mall.MallGoodsInfo{})
	if req.CategoryId != 0 {
		db = db.Where("category_id = ?", req.CategoryId)
	}
	if req.StockNum != nil {
		db = db.Where("stock_num = ?", req.StockNum)
	}
	if req.Type != 0 {
		db = db.Where("type = ?", req.Type)
	}
	if req.SellStatus != 0 {
		db = db.Where("sell_status = ?", req.SellStatus)
	}
	err = db.Count(&total).Error
	return total, err
}
